﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Core;
using Jw.Models;

namespace Jw.Web.Areas.Basic.Controllers
{
    [MvcMenuFilter(false)]
    public class HomeController : Controller
    {
        //
        // GET: /Basic/Home/
        [System.Web.Mvc.AllowAnonymous]
        public ActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
                return Redirect("~/Login/mms");

            return View();
        }

        /// <summary>
        /// 物料字典弹出窗口
        /// </summary>
        /// <returns></returns>
        public ActionResult LookupWlzd()
        {
            return View();
        }
    }

    public class HomeApiController : ApiController
    {
        //弹出物料字典窗口数据
        public dynamic GetWlzd(RequestWrapper request)
        {
            request.LoadSettingXmlString(@"
            <settings defaultOrderBy='Wlzd_Wlbh DESC'>
            <select> A.* </select>
                <from>
                    l_Wlzd A
                </from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Wlzd_Wlbh'     cp='like'></field>
                        <field name='Wlzd_Wlmc'     cp='like'></field>
                    </where>
            </settings>");
            var service = new l_WlzdService();
            var pQuery = request.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }

        //弹出物料字典类别窗口数据
        public dynamic GetWlzdType()
        {
            var service = new l_WllbService();
            var requst = RequestWrapper.Instance()
                .LoadSettingXmlString(@"
            <settings defaultOrderBy='Wllb_Lbbh'>
                <select>
                    Wllb_Lbmc as text,Wllb_Lbbh as id,Wllb_Sjbh as pid
                </select>
                <from>
                    l_Wllb
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                </where>
            </settings>");

            var pQuery = requst.ToParamQuery();

            //pQuery.AndWhere("MaterialType", "", x => {
            //    var data = request.ToParamQuery().GetData();
            //    var from = data.From.IndexOf(' ')>-1 ? string.Format("({0})", data.From) : data.From;
            //    var where = data.WhereSql;
            //    where = string.IsNullOrEmpty(where)? string.Empty:("where " + where);
            //    var sql = string.Format("MaterialType in (select MaterialType from {0} {1})", from, where);
            //    return sql;
            //});

            return service.GetDynamicList(pQuery);
        }
    }

}
